home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12519 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help - sorting strings
  5. Date: Mon, 01 Apr 96 13:13:27 GMT
  6. Organization: none
  7. Message-ID: <828364407snz@genesis.demon.co.uk>
  8. References: <4j11ne$kj9$1@mhafn.production.compuserve.com> <4j67ki$hvr@transformer.pti-us.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4j67ki$hvr@transformer.pti-us.com>
  15.            wv.dixon@pti-us.com "Walt Dixon" writes:
  16.  
  17. >#include <stdlib.h>
  18. >
  19. >static int alphabetize( const void* arg1, const void* arg2 )
  20. >{
  21. >    return strcmp(*(char**)arg1, *(char**)arg2));
  22. >}
  23.  
  24. I suggest you write that as:
  25.  
  26.      return strcmp(*(char*const*)arg1, *(char*const*)arg2));
  27.  
  28. While you can cast const away don't do it unless you have to. If nothing else
  29. in this case some compilers can warn about asting qualifiers away so the
  30. version with const has more chance of compiling cleanly.
  31.  
  32. >
  33. >you pass a pointer to your sorting function to qsort.  qsort will then
  34. >envoke your function as required to sort the array of strings.
  35. >Here is how you would envoke the routine.
  36. >
  37. >qsort(words,num_elements,sizeof(char*),alphabetize);
  38. >
  39. >words is declared to be a char**, num_elements is the number of strings,
  40. >sizeof(char*) is the size of each element in the char* array, and alphabetize
  41. >is the pointer to the comparison funciton.
  42. >
  43. >To reverse sort all you would have to do is to reverse the two arguments
  44. >to the strcmp function.
  45. >
  46. >I have attached a routine that reads in a bunch of strings.  One string
  47. >per line, sorts then and prints them out.  I did not clean up
  48. >the memory that was allocated.  It is only an example.
  49. >
  50. >walt
  51. >
  52. >---------------------------------240801314710921
  53. >Content-Transfer-Encoding: 7bit
  54. >Content-Type: text/plain
  55. >
  56. >#include <stdlib.h>
  57. >#include <stdio.h>
  58. >#include <string.h>
  59. >
  60. >int compare(const void* a, const void* b)
  61. >{
  62. >    return(strcmp(*(char**)b,*(char**)a));
  63. >}
  64.  
  65. I suggest you write that as:
  66.  
  67.      return(strcmp(*(char*const*)b,*(char*const*)a));
  68.  
  69. While you can cast const away don't do it unless you have to. If nothing else
  70. in this case some compilers can warn about asting qualifiers away so the
  71. version with const has more chance of compiling cleanly.
  72.  
  73. >void main(int argc, char** argv)
  74.   ^
  75.  int 
  76.  
  77. >{
  78. >    FILE* fh;
  79. >    char** words;
  80. >    char buffer[132];
  81. >    int i,ii;
  82. >
  83. >    if(argc != 2)
  84. >    {
  85. >        printf("No file specified.\n");
  86. >        exit(1);
  87.  
  88. 1 as an exit status has no defined meaning - use EXIT_FAILURE to indicate
  89. a failure.
  90.  
  91. >    }
  92. >    fh = fopen(argv[1],"r");
  93. >    if(fh)
  94. >    {
  95. >        words = (char**) malloc(500 * sizeof(char*));
  96. >        i = 0;
  97. >        while(fscanf(fh,"%s \n",buffer)==1)
  98. >        {
  99. >            words[i++] = strdup(buffer); 
  100.  
  101. strdup() isn't a standard library function - this simply won't compile/link
  102. on many systems.
  103.  
  104. >        }
  105. >        printf("read %d words\n",i);
  106. >        qsort(words,i,sizeof(char*),compare);
  107. >        for(ii=0; ii < i; ii++)
  108. >        {
  109. >            printf("%s\n",words[ii]);
  110. >        }
  111. >        fclose(fh);
  112. >    }
  113. >    else
  114. >    {
  115. >        printf("Unable to open %s\n",argv[1]);
  116. >    }
  117.  
  118.      return 0; /* Or return EXIT_SUCCESS */
  119. >}
  120.  
  121. -- 
  122. -----------------------------------------
  123. Lawrence Kirby | fred@genesis.demon.co.uk
  124. Wilts, England | 70734.126@compuserve.com
  125. -----------------------------------------
  126.